package hellfire.system.graphics;

/**
 * Vertex shader codes start with 'v'.
 * Fragment shader codes start with 'f'.
 * Programs (vertex+fragment pairs) start with 'p'
 * @author Roma
 *
 */
public class Shaders 
{	
	
	public final static String vATOM =
			"uniform mat4 uMVPMatrix;" +
			"attribute vec4 aPosition;" +
			"void main() " +
			"{" +
			"	gl_Position = uMVPMatrix * aPosition;" +
			"}";
	
	public final static String fBUCKETFILL =
			"precision mediump float;" +
			"uniform vec4 uColor;" +
			"void main() " +
			"{" + 
			"	gl_FragColor = uColor;" +
			"}";
	
	public final static String vSMARTATOM =
			"uniform mat4 uMVPMatrix;" +
			"attribute vec4 aPosition;" +
			"attribute vec2 aTexCoord;" +
			"varying vec2 vTexCoord;" +
			"void main() " +
			"{" +
			"   vTexCoord = aTexCoord;" +
			"	gl_Position = uMVPMatrix * aPosition;" +
			"}";
	
	
	public final static String fPICASSO =
			"precision mediump float;" +
			"uniform vec4 uColor;" +
			"uniform sampler2D uTexture;" +
			"varying vec2 vTexCoord;" +
			"void main() " +
			"{" + 
			"   vec4 pxl = texture2D(uTexture, vTexCoord);" +
			"	gl_FragColor = pxl * uColor;" +
			"}";
	
	public final static String fBRAIN =
			"precision mediump float;" +
			"uniform float uLength;" +
			"uniform vec4 uColor;" +
			"uniform vec4 uBackground;" +
			"uniform sampler2D uFontTexture;" +
			"uniform sampler2D uString;" +
			"varying vec2 vTexCoord;" +
			"void main() " +
			"{" + 
			"	vec2 char_pos = vec2(vTexCoord.x, 0);" + 
			"   int char = int(texture2D(uString, char_pos).a*256.0f);" +
			"	int row = char / 16;" +
			"	int col = char - row * 16;" +
			//"	row = 15 - row;" + 
			"	float tex_x = float(col) / 16.0f + 0.015;" + 
			"	float tex_y = float(row) / 16.0f;" + 
			"	vec2 pos = vec2(tex_x + mod(vTexCoord[0]*0.0325f*uLength, 0.0325f), tex_y + vTexCoord[1]/16.0f); " +  
			"	vec4 pxl = texture2D(uFontTexture, pos);" +
			"	pxl = pxl.a*pxl*uColor + (1.0 - pxl.a)*uBackground;" + 
			"	gl_FragColor = pxl;" +
			"}";
	
	
	public final static String fLEV =
			"precision mediump float;" +
			"uniform vec4 uColor;" +
			"uniform sampler2D uTexture;" +
			"uniform int uSpriteId;" +
			"uniform ivec2 uAtlasDims;" +
			"varying vec2 vTexCoord;" +
			"void main() " +
			"{" + 
			"	int row = uSpriteId / uAtlasDims[0];" + 
			"	int col = uSpriteId - row * uAtlasDims[0];" +
			//"	row = uAtlasDims[1] - 1 - row;" +  
			"	vec2 fAtlasDims = vec2(float(uAtlasDims[0]), float(uAtlasDims[1]));" +
			"	float tex_x = float(col) / fAtlasDims[0];" + 
			"	float tex_y = float(row) / fAtlasDims[1];" + 
			"	vec2 pos = vec2(tex_x + vTexCoord[0]/fAtlasDims[0], tex_y + vTexCoord[1]/fAtlasDims[1]);" +
			"   vec4 pxl = texture2D(uTexture, pos);" +
			"	gl_FragColor = pxl * uColor;" +
			"}";
	
	public final static String vPARTICLES = 
			"uniform mat4 uMVPMatrix;" +
			"uniform float uSimTime;" +
			"attribute vec2 aVelocity;" +
			"attribute vec4 aColor;" +
			"attribute float aSize;" +
			"attribute vec4 aPosition;" +
			"varying vec4 vColor;" +
			"void main() " +
			"{" +
			"	vec2 dpos_ = uSimTime*aVelocity;" +
			"	vec4 dpos = vec4(dpos_.x, dpos_.y, 0.0f, 0.0f);" + 
			"	gl_Position = uMVPMatrix * (aPosition+dpos); " +
			"	vColor = aColor;" +
			"	gl_PointSize = aSize;" + 
			"}";
	
	public final static String fFIREWORKS = 
			"varying vec4 vColor;" + 
			"void main()" +
			"{" + 
			"	vec2 distVector = 2.0f*(gl_PointCoord - vec2(0.5f, 0.5f));" + 
			"	float dist = length(distVector);" + 
			"	gl_FragColor = vColor*vec4(1.0f,1.0f,1.0f,1.0f-dist/5.0f);" + 
			"}";
			
	
	public final static String[] pRect = { vATOM, fBUCKETFILL };
	
	public final static String[] pSpark = { vPARTICLES, fFIREWORKS };
	
	public final static String[] pImage = { vSMARTATOM, fPICASSO };
	
	public final static String[] pString = { vSMARTATOM, fBRAIN };
	
	public final static String[] pAtlas = { vSMARTATOM, fLEV };
	
	public final static String[][] progList = { pRect, pImage, pString, pAtlas, pSpark }; 

}
